//    Copyright (C) Kherty.  All rights reserved.
using System.Windows.Media.Effects;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Internals;

namespace OpenLS.Drawing
{
    public class OuterShadowEffect : ImageEffect
    {
        private RectAlignment alignment;
        private int blurRad;
        private ColorBase color;
        private double dir;
        private double dist;
        private int kx;
        private int ky;
        private bool rotWithShape = true;
        private int sb;
        private int sy;

        public bool RotateWithShape
        {
            get { return rotWithShape; }
            set { rotWithShape = value; }
        }

        public RectAlignment Alignment
        {
            get { return alignment; }
            set { alignment = value; }
        }

        public int Ky
        {
            get { return ky; }
            set { ky = value; }
        }

        public int Kx
        {
            get { return kx; }
            set { kx = value; }
        }

        public int Sy
        {
            get { return sy; }
            set { sy = value; }
        }

        public ColorBase Color
        {
            get { return color; }
            set { color = value; }
        }

        public int BlurRad
        {
            get { return blurRad; }
            set { blurRad = value; }
        }

        public double Distance
        {
            get { return dist; }
            set { dist = value; }
        }

        public double Direction
        {
            get { return dir; }
            set { dir = value; }
        }

        public int Sx
        {
            get { return sb; }
            set { sb = value; }
        }


        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("outerShdw"))
            {
                BlurRad = c.GetOptionalInteger("blurRad", 0);
                Distance = EmuConverter.FromEmu(c.GetOptionalInteger64("dist", 0));
                Direction = FromAngle(c.GetOptionalInteger("dir", 0));
                Sx = c.GetOptionalInteger("sx", 100000);
                Sy = c.GetOptionalInteger("sy", 100000);
                Kx = c.GetOptionalInteger("kx", 0);
                Ky = c.GetOptionalInteger("ky", 0);
                alignment = c.GetOptionalEnum("algn", RectAlignment.Top);
                RotateWithShape = c.GetOptionalBoolean("rotWithShape", true);

                Color = ColorBase.ReadColor(c);
            }
        }

        private static double FromAngle(int i)
        {
            return -((double) i)/60000;
        }

        private static int ToAngle(double i)
        {
            return -(int) (i*60000);
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("outerShdw"))
            {
                c.SetOptionalInteger("blurRad", BlurRad, 0);
                c.SetOptionalInteger64("dist", EmuConverter.ToEmu(Distance), 0);
                c.SetOptionalInteger("dir", ToAngle(Direction), 0);
                c.SetOptionalInteger("sx", Sx, 100000);
                c.SetOptionalInteger("sy", Sy, 100000);
                c.SetOptionalInteger("kx", Ky, 0);
                c.SetOptionalInteger("ky", Ky, 0);
                c.SetOptionalEnum("algn", Alignment, RectAlignment.Top);
                c.SetOptionalBoolean("rotWithShape", RotateWithShape, true);
                c.WriteElement(Color);
            }
        }

        public override BitmapEffect BitmapEffect(ColorScheme colorScheme)
        {
            {
                var result = new DropShadowBitmapEffect
                                                    {
                                                        Color = Color.ResolveColor(colorScheme),
                                                        Direction = Direction,
                                                        Noise = 0,
                                                        Softness = 0,
                                                        ShadowDepth = Distance
                                                    };
                return result;
            }
        }
    }
}